package com.ruoyi.business.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.domain.CarMaintenanceInfo;
import com.ruoyi.business.domain.CarPackageAudit;
import com.ruoyi.business.domain.HistoricActivity;
import com.ruoyi.business.domain.ServiceItem;
import com.ruoyi.business.service.ICarPackageAuditService;
import com.ruoyi.business.service.IProcessService;
import com.ruoyi.business.service.IServiceItemService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ShiroUtils;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.image.ProcessDiagramGenerator;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

/**
 * Created by lanxw
 */
@Controller
@RequestMapping("/business/carPackageAudit")
public class CarPackageAuditController extends BaseController {
    private String prefix = "business/carPackageAudit";
    @Autowired
    private ICarPackageAuditService carPackageAuditService;
    @Autowired
    private IServiceItemService serviceItemService;
    @Autowired
    private IProcessService processService;
    @RequiresPermissions("business:carPackageAudit:view")
    @GetMapping()
    public String carMaintenanceInfo()
    {
        return prefix + "/carPackageAudit";
    }
    /**
     * 查询套餐审核列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CarPackageAudit carPackageAudit)
    {
        startPage();
        carPackageAudit.setCreateBy(ShiroUtils.getUserId().toString());
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        return getDataTable(list);
    }
    @RequestMapping(value = "/processImg/{id}")
    public void processImg(@PathVariable("id") String id, HttpServletResponse response)
            throws Exception {
        CarPackageAudit carPackageAudit = carPackageAuditService.getCarPackageAudit(id);
        InputStream inputStream=null;
        if(carPackageAudit.getStatus().equals(CarPackageAudit.STATUS_IN_ROGRESS) ||
                carPackageAudit.getStatus().equals(CarPackageAudit.STATUS_REJECT)
        ){
            //获取活动节点
            List<String> activeActivityIds = processService.getActiveActivityIds(carPackageAudit.getInstanceId());
            inputStream = processService.getProcessImage(carPackageAudit.getProcessDefinitionId(),activeActivityIds,Collections.EMPTY_LIST);
        }else{
            inputStream = processService.getProcessImage(carPackageAudit.getProcessDefinitionId(),Collections.EMPTY_LIST,Collections.EMPTY_LIST);
        }
        IOUtils.copy(inputStream,response.getOutputStream());
    }
    @PostMapping( "/cancelApply")
    @ResponseBody
    public AjaxResult cancelApply(String instanceId) {
        carPackageAuditService.cancelApply(instanceId);
        return success();
    }
    @GetMapping("/todoPage")
    public String todoPage()
    {
        return prefix + "/todoPage";
    }
    @PostMapping("/todoList")
    @ResponseBody
    public TableDataInfo todoList(CarPackageAudit carPackageAudit)
    {
        List<CarPackageAudit> list = carPackageAuditService.findTodoList(carPackageAudit);
        return getDataTable(list);
    }
    @RequestMapping("/showVerifyDialog/{taskId}")
    public String showVerifyDialog(@PathVariable("taskId") String taskId, ModelMap mmap){
        mmap.put("taskId",taskId);
        return prefix+"/taskVerify";
    }
    /**
     * 完成任务
     *
     * @return
     */
    @RequestMapping(value = "/complete/{taskId}", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxResult complete(@PathVariable("taskId") String taskId,
                               @RequestParam(value = "auditStatus") String auditStatus,
                               @RequestParam(value = "comment") String comment)
    {
        carPackageAuditService.complate(taskId,auditStatus,comment);

        return success("任务已完成");
    }
    @GetMapping("/openServiceItemsEditPage/{carPackageAuditId}")
    public String edit(@PathVariable("carPackageAuditId") String carPackageAuditId, ModelMap mmap)
    {
        CarPackageAudit audit = carPackageAuditService.getCarPackageAudit(carPackageAuditId);
        ServiceItem serviceItem = JSON.parseObject(audit.getServiceItemInfo(),ServiceItem.class);
        mmap.put("serviceItem", serviceItem);
        mmap.put("carPackageAuditId", carPackageAuditId);
        return prefix + "/serviceItemEdit";
    }
    @PostMapping("/serviceItemUpdate")
    @ResponseBody
    public AjaxResult editSave(String carPackageAuditId,ServiceItem serviceItem)
    {
        carPackageAuditService.updateServiceItem(carPackageAuditId,serviceItem);
        return success();
    }
    @PostMapping( "/reApply")
    @ResponseBody
    public AjaxResult reApply(String taskId,String carPackageAuditId) {
        carPackageAuditService.reApply(taskId,carPackageAuditId);
        return success();
    }
    @GetMapping("/donePage")
    public String donePage()
    {
        return prefix + "/donePage";
    }
    @PostMapping("/doneList")
    @ResponseBody
    public TableDataInfo doneList(CarPackageAudit carPackageAudit)
    {
        List<CarPackageAudit> list = carPackageAuditService.findDoneList(carPackageAudit);
        return getDataTable(list);
    }
    /**
     * 加载审批历史弹窗
     */
    /*第五步:控制器*/
    @GetMapping("/historyList/{instanceId}")
    public String historyList(@PathVariable("instanceId") String instanceId, ModelMap mmap)
    {
        mmap.put("instanceId", instanceId);
        return prefix + "/historyList";
    }
    @PostMapping("/listHistory")
    @ResponseBody/*第七步*/
    public TableDataInfo listHistory(String instanceId) {
        startPage();
        List<HistoricActivity> list = carPackageAuditService.selectHistoryList(instanceId);
        return getDataTable(list);
    }
}
